package server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author by mozping
 * @Classname NettyServerInHandlerOne
 * @Description TODO
 * @Date 2019/4/2 19:03
 */
public class NettyServerInHandlerOne extends ChannelInboundHandlerAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(NettyServerInHandlerOne.class);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        LOG.info("Channel InboundHandlerOne channelRead 执行...");
        ByteBuf result = (ByteBuf) msg;
        String msgStr = result.toString(CharsetUtil.UTF_8);
        LOG.info("Client said:{}", msgStr);
        ByteBuf newByteBuf = Unpooled.copiedBuffer(msgStr + "NettyServerInHandlerOne ", CharsetUtil.UTF_8);
        //在inChannel里一旦调用write方法，就会返回使用outChannel进行处理，后面的inChannel就不能继续处理了，
        // 因此inChannel不能调用write方法
        //ctx.write(newByteBuf);
        ctx.fireChannelRead(newByteBuf);

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        LOG.info("Channel InboundHandlerOne channelReadComplete 执行...");
        ctx.flush();
    }
}
